import {
  getTaskTable,
  editTaskListRecord
} from '../../api/task'
import {
  TASK_LIST,
  TASK_LIST_CURRENT_ROW,
  TASK_LIST_DATA_COUNT,
  TASK_LIST_HISTORY_DATA,
  TASK_LIST_PAGE_SIZE,
  CURRENT_PAGE
} from '../motations-type'
import qqgroup from '@/assets/images/qq-group1.jpg'

export default {
  state: {
    taskList: [],
    taskListDataCount: null,
    taskListPageSize: 20,
    imgPageSize: 8,
    taskListHistoryData: [],
    currentPage: 1,
    row: {
      ctime: '',
      time: '',
      cid: '',
      type: '',
      method: '',
      task: '',
      taskRequire: '',
      budget: '',
      traffic: '',
      contentType: '',
      sticky: false,
      // open: false,
      missionMoney: '',
      price: '',
      commision: '',
      subtitle: '',
      taskmoney: '',
      order: '',
      tcimg: '',
      openState: false,
      strickState: false,
      state: '',
      top: false
    }
  },
  getters: {
    taskList: state => state.taskList
  },
  mutations: {
    [TASK_LIST] (state, data) {
      state.taskList = data
    },
    [TASK_LIST_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [TASK_LIST_DATA_COUNT] (state, data) {
      state.taskListDataCount = data
    },
    [TASK_LIST_HISTORY_DATA] (state, data) {
      state.taskListHistoryData = data
    },
    [TASK_LIST_PAGE_SIZE] (state, data) {
      state.taskListPageSize = data
    },
    [CURRENT_PAGE] (state, data) {
      state.currentPage = data
    }
  },
  actions: {
    async getTaskList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getTaskTable(data)
      // console.log(resp)
      let host = resp.data.host
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          host: host,
          id: item.id,
          cid: item.cid,
          time: [item.btime, item.etime],
          ctime: item.ctime,
          btime: item.btime,
          etime: item.etime,
          type: item.type.toString(),
          name: item.name,
          cname: item.cname,
          des: item.des,
          budget: item.budget,
          // traffic: '',
          contentType: '',
          sticky: false,
          openState: item.state === 1,
          strickState: item.top !== 'false',
          state: item.state,
          permoney: item.permoney,
          perrate: item.perrate,
          sort: item.sort,
          subtitle: item.subtitle,
          top: item.top,
          tctype: item.tctype && item.tctype !== 0 ? item.tctype.toString() : item.tctype,
          tcimg: item.tcimg
        }]
      })
      commit(TASK_LIST, newData)
      commit(TASK_LIST_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.taskList.taskListPageSize) {
        commit(TASK_LIST_HISTORY_DATA, newData)
      } else {
        commit(TASK_LIST_HISTORY_DATA, newData.slice(0, rootState.taskList.taskListPageSize))
      }
      return resp
    },
    async editTaskListData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editTaskListRecord(data)
      return resp
    },
    async getTaskListCurrentRow ({ commit, rootState }, row) {
      commit(TASK_LIST_CURRENT_ROW, row)
    },
    async changeTaskListPage ({ commit, rootState }, index) {
      let data = rootState.taskList.taskList
      let _start = (index - 1) * rootState.taskList.taskListPageSize
      let _end = index * rootState.taskList.taskListPageSize
      commit(TASK_LIST_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeTaskListPageSize ({ commit, rootState }, value) {
      commit(TASK_LIST_PAGE_SIZE, value)
    },
    async changeCurrentPage ({ commit, rootState }, value) {
      commit(CURRENT_PAGE, value)
    }
  }
}
